package org.tom.user.controller;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tom.basic.exception.MyException;
import org.tom.basic.util.JsonResult;
import org.tom.user.dto.LoginDto;
import org.tom.user.service.ILoginService;
import org.tom.user.service.ILogininfoService;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/accout")
    public JsonResult login(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map =  loginService.login(loginDto);
            return JsonResult.me().setResultObj(map);
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }

    }

    /*
    * 微信登录
    * */
    @PostMapping("/wechat")
    public JsonResult wechat(@RequestBody Map<String,String> params){ // 用map接收参数
        try {
            return loginService.loginWechat(params);
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @PostMapping("/binder/wechat")
    public JsonResult binder(@RequestBody Map<String,String> params){
        try {
            return loginService.binderWechat(params);
        } catch (MyException e){
          e.printStackTrace();
          return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }
    }
}
